<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="属性编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入商品属性编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SKU" prop="skuId">
        <el-select v-model="queryParams.skuId" placeholder="请选择SKU" clearable
                   size="small">
          <el-option
            v-for="item in skuList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable
                   size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['goods:goods_spec:add']">
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['goods:goods_spec:remove']">
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-col>
    </el-row>

    <el-row class="mb8" v-if="thisGoods != null">
      所属商品：{{thisGoods.goodsName}}
    </el-row>

    <el-table :cell-style="{padding:'0px'}" v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="规格" align="center" prop="specsName"/>
      <el-table-column label="商品属性编号" align="center" prop="number"/>
      <el-table-column label="图片" align="center" prop="image">
        <template slot-scope="scope">
          <el-image
            style="width: 80px; height: 80px"
            :src="scope.row.image"
            :preview-src-list="[scope.row.image]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="原价" align="center" prop="originalPrice"/>
      <el-table-column label="现价" align="center" prop="presentPrice"/>
      <el-table-column label="当前库存总数" align="center" prop="inventoryTotal"/>
      <el-table-column label="已售总数" align="center" prop="soldoutTotal"/>
      <el-table-column label="状态" align="center" prop="status"
                       :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button size="mini" type="text" icon="el-icon-edit" @click="updateInventory(scope.row)"
                     v-hasPermi="['goods:goods_spec:edit']">
            更新库存
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['goods:goods_spec:edit']">
            修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['goods:goods_spec:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"/>

    <add-or-update v-show="open" @on-close="open=false" ref="addOrUpdate"></add-or-update>

    <el-dialog :close-on-click-modal="false" title="库存 增加/减少" :visible.sync="open2" @close="cancel2"
               width="500px" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules2" label-width="150px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="更变数量(负数为减少)" prop="practicalMoney">
              <el-input-number v-model="form2.inventoryTotal" :precision="0" :step="1" :max="99999999"
                               placeholder="负数为减少库存"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listGoods_spec,
    getGoods_spec,
    delGoods_spec,
    exportGoods_spec,
    updateInventory
  } from '@/api/goods/goods_spec'
  import mixinViewModule from '@/mixins/view-module'
  import AddOrUpdate from './add-or-update'
  import {
    skuList
  } from '@/api/goods/goods'

  import {
    getGoods
  } from '@/api/goods/goods'

  export default {
    name: 'Goods_spec',
    mixins: [mixinViewModule],
    components: { AddOrUpdate },
    data() {
      return {
        mixinViewModuleOptions: {
          getDetailsURL: getGoods_spec,// 查询详情实体
          getDataListURL: listGoods_spec, // 数据列表接口，API地址
          deleteURL: delGoods_spec, // 删除接口，API地址
          tableKey: 'id', // 删除接口，批量状态下由那个key进行标记操作？比如：pid，uid...
          exportURL: exportGoods_spec // 导出接口，API地址
        },
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 状态;0=已上架,1=未上架字典
        statusOptions: [],
        goodsId: null,
        skuList: [],
        thisGoods: null,

        open2: false,
        form2: {
          inventoryTotal: null
        },
        rules2: [],
        selectItem: null,

        // 查询参数
        queryParams: {
          goodsId: null,
          number: null,
          originalPrice: null,
          presentPrice: null,
          inventoryTotal: null,
          soldoutTotal: null,
          status: null
          , orderByColumn: 't.create_time'
          , isAsc: 'desc'
        }
      }

    },
    created() {
      const goodsId = this.$route.params && this.$route.params.goodsId
      this.goodsId = goodsId
      this.getList()
      skuList({ goodsId: goodsId }).then(response => {
        this.skuList = response.data
      })
      this.getDicts('goods_status').then(response => {
        this.statusOptions = response.data
        this.$refs.addOrUpdate.statusOptions = response.data
      })
      getGoods(goodsId).then(response => {
        this.thisGoods = response.data
      })
    },
    methods: {
      updateInventory(item) {
        this.form2 = {
          inventoryTotal: null
        }
        this.open2 = true
        this.selectItem = item
      },
      submitForm2() {
        let _this = this
        this.$refs['form2'].validate(valid => {
          if (valid) {
            updateInventory({ id: _this.selectItem.id, inventoryTotal: _this.form2.inventoryTotal }).then(response => {
              _this.msgSuccess('更新成功')
              _this.cancel2()
              _this.getList()
            })
          }
        })
      },
      cancel2() {
        this.open2 = false
        this.selectItem = null
      },
      // 状态;0=已上架,1=未上架字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status)
      },
      //打开dislog前执行
      onOpenHandleDialog() {
        this.$refs.addOrUpdate.goodsId = this.goodsId
        this.$refs.addOrUpdate.initHttp()
      },
      //打开删除前执行
      onHandleDetails(data, type) {
        if (type == this.$formType.edit) {
          this.$refs.addOrUpdate.form = data.data
        }
      },
      //开始查询列表的时候
      onStartQuery() {
        this.queryParams.goodsId = this.goodsId
      },
      //开始重制查询前执行
      onResetQuery() {

      }
    }
  }
</script>
